package card_discount

import (
	"fmt"
	// "bp/cache"
	sqlc "bp/db/sqlc"
	"bp/db/sqlc/ctype"
	"bp/internal/util"
	"context"
	"time"
)

// CreateCardDiscountParam 新建卡折扣参数
type CreateCardDiscountParam struct {
	UpsertCardDiscountParamsPtr *sqlc.UpsertCardDiscountParams
}

// CreateCardDiscount 新建卡折扣
func (s *CardDiscountEntityServiceImp) CreateCardDiscount(ctx context.Context, q *sqlc.Queries, ptr *CreateCardDiscountParam) (cardDiscountPtr *sqlc.CardDiscount, err error) {
	slog := util.NewContextLogger(ctx, s.slog)

	cardDiscount, err := q.UpsertCardDiscount(ctx, *ptr.UpsertCardDiscountParamsPtr)
	if err != nil {
		return
	}
	slog.Infof("1 CreateCardDiscount ok")

	cardDiscountPtr = &cardDiscount
	return
}

// UpdateCardDiscountParam 更新卡折扣参数
type UpdateCardDiscountParam struct {
	CardDiscountID          int64
	Name                    string
	Desc                    string
	Tags                    []string
	OfferCategory           string
	Discount                int32
	NeedVerifyQualification bool
	QualifyTypes            []string
	UseTimeRanges           ctype.TimeRangeArray
	DateChooseType          string
	Weekdays                []int32
	Monthdays               []int32
	UseScenes               []string
	Scope                   string
}

// UpdateCardDiscount 更新卡折扣
func (s *CardDiscountEntityServiceImp) UpdateCardDiscount(ctx context.Context, q *sqlc.Queries, ptr *UpdateCardDiscountParam) (cardDiscountPtr *sqlc.CardDiscount, err error) {
	slog := util.NewContextLogger(ctx, s.slog)

	CardDiscountID := ptr.CardDiscountID
	cardDiscountPtr, err = s.GetCardDiscountPtr(ctx, q, CardDiscountID)
	if err != nil {
		return
	}
	slog.Infof("1 GetCardDiscountPtr ok")

	cardDiscount, err := q.UpdateCardDiscount(ctx, sqlc.UpdateCardDiscountParams{
		ID:                      CardDiscountID,
		Name:                    ptr.Name,
		Desc:                    sqlc.NSFromString(ptr.Desc),
		Tags:                    ptr.Tags,
		OfferCategory:           ptr.OfferCategory,
		Discount:                ptr.Discount,
		NeedVerifyQualification: ptr.NeedVerifyQualification,
		QualifyTypes:            ptr.QualifyTypes,
		UseTimeRanges:           ptr.UseTimeRanges,
		DateChooseType:          sqlc.NSFromString(ptr.DateChooseType),
		Weekdays:                ptr.Weekdays,
		Monthdays:               ptr.Monthdays,
		UseScenes:               ptr.UseScenes,
		Scope:                   ptr.Scope,
	})
	if err != nil {
		return
	}
	slog.Infof("3 UpdateCardDiscount ok")

	cardDiscountPtr = &cardDiscount
	return
}

// MarkCardDiscountParam 标记卡折扣参数
type MarkCardDiscountParam struct {
	CardDiscountPtr *sqlc.CardDiscount
	CardDiscountID  int64
}

// MarkCardDiscountPaused 标记卡折扣停用
func (s *CardDiscountEntityServiceImp) MarkCardDiscountPaused(ctx context.Context, q *sqlc.Queries, ptr *MarkCardDiscountParam) (cardDiscountPtr *sqlc.CardDiscount, err error) {
	slog := util.NewContextLogger(ctx, s.slog)

	if ptr.CardDiscountPtr != nil {
		cardDiscountPtr = ptr.CardDiscountPtr
	} else {
		cardDiscountPtr, err = s.GetCardDiscountPtr(ctx, q, ptr.CardDiscountID)
		if err != nil {
			return
		}
		slog.Infof("0 GetCardDiscountPtr ok")
	}
	if cardDiscountPtr.IsPaused {
		err = fmt.Errorf("卡折扣已停用")
		return
	}
	cardDiscountID := cardDiscountPtr.ID

	err = q.MarkCardDiscountPaused(ctx, cardDiscountID)
	if err != nil {
		return
	}
	cardDiscountPtr.IsPaused = true
	cardDiscountPtr.Paused = sqlc.NTimeFromTime(time.Now())
	slog.Infof("1 PauseCoupon ok")

	return
}

// UnMarkCardDiscountPaused 取消标记卡折扣停用
func (s *CardDiscountEntityServiceImp) UnMarkCardDiscountPaused(ctx context.Context, q *sqlc.Queries, ptr *MarkCardDiscountParam) (cardDiscountPtr *sqlc.CardDiscount, err error) {
	slog := util.NewContextLogger(ctx, s.slog)

	if ptr.CardDiscountPtr != nil {
		cardDiscountPtr = ptr.CardDiscountPtr
	} else {
		cardDiscountPtr, err = s.GetCardDiscountPtr(ctx, q, ptr.CardDiscountID)
		if err != nil {
			return
		}
		slog.Infof("0 GetCardDiscountPtr ok")
	}
	if !cardDiscountPtr.IsPaused {
		err = fmt.Errorf("卡折扣正常")
		return
	}
	cardDiscountID := cardDiscountPtr.ID

	err = q.UnMarkCardDiscountPaused(ctx, cardDiscountID)
	if err != nil {
		return
	}
	cardDiscountPtr.IsPaused = false
	slog.Infof("1 UnMarkCardDiscountPaused ok")

	return
}
